package echat.future.cn.echat.home

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.zhusx.core.app._BaseFragment
import echat.future.cn.echat.R
import kotlinx.android.synthetic.main.fragment_home.*

/**
 * Created by Administrator on 2018/8/9.
 */

class HomeFragment : _BaseFragment() {
    val popupXJ by lazy {
        ChoosePopup(activity!!, 0, object : ChoosePopup.OnItemCheckedListener {
            override fun onChecked(type: Int, index: Int) {
                fragment[2].refresh(index)
            }
        })
    }
//    val popupXB by lazy {
//        ChoosePopup(activity!!, 1, object : ChoosePopup.OnItemCheckedListener {
//            override fun onChecked(type: Int, index: Int) {
//                fragment[4].refresh(index)
//            }
//        })
//    }
    var currentIndex = 0
    val fragment = Array(6) {
        val f = HomeItemFragment()
        f.index = it
        f
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
//        radio_xingji.setOnClickListener {
//            if (currentIndex == 2) {
//                popupXJ.showAsDropDown(it)
//            }
//        }
//        radio_xingbie.setOnClickListener {
//            if (currentIndex == 4) {
//                popupXB.showAsDropDown(it)
//            }
//        }
        iv_search.setOnClickListener { startActivity(Intent(activity, SearchActivity::class.java)) }
        radioGroup.check(R.id.radio_tuijian)
        viewPager.adapter = object : FragmentPagerAdapter(childFragmentManager) {
            override fun getItem(position: Int): Fragment {
                return fragment[position]
            }

            override fun getCount(): Int = 6
        }
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            if (checkedId != -1) {
                var index = 0
                when (checkedId) {
                    R.id.radio_tuijian -> {
                        index = 0
                    }
                    R.id.radio_xinren -> {
                        index = 1
                    }
//                    R.id.radio_shoumian -> {
//                        index = 2
//                    }
                    R.id.radio_guanzhu -> {
                        index = 2
                    }
//                    R.id.radio_xingbie -> {
//                        index = 4
//                    }
                    R.id.radio_sanxing -> {
                        index = 3
                    }
                    R.id.radio_sixing -> {
                        index = 4
                    }
                    R.id.radio_wuxing -> {
                        index = 5
                    }
                }
                if (viewPager.currentItem != index) {
                    viewPager.setCurrentItem(index, false)
                }
            }
        }
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                currentIndex = position
                var checkId = R.id.radio_tuijian
                when (position) {
                    0 -> {
                        checkId = R.id.radio_tuijian
                    }
                    1 -> {
                        checkId = R.id.radio_xinren
                    }
//                    2 -> {
//                        checkId = R.id.radio_shoumian
//                    }
                    2 -> {
                        checkId = R.id.radio_guanzhu
                    }
                    3 -> {
                        checkId = R.id.radio_sanxing
                    }
                    4 -> {
                        checkId = R.id.radio_sixing
                    }
                    5 -> {
                        checkId = R.id.radio_wuxing
                    }
                }
                if (radioGroup.checkedRadioButtonId != checkId) {
                    radioGroup.check(checkId)
                }

            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
        viewPager.offscreenPageLimit = 6
    }
}
